home *** CD-ROM | disk | FTP | other *** search
/ Experimental BBS Explossion 3 / Experimental BBS Explossion III.iso / games / nhak_src.zip / MACINIT.C < prev    next >
C/C++ Source or Header  |  1993-03-16  |  12KB  |  498 lines

  1. /*    SCCS Id: @(#)macinit.c    3.0    88/08/05
  2. /* Copyright (c) Johnny Lee, 1989.         */
  3. /* NetHack may be freely redistributed.  See license for details. */
  4.  
  5. /*    Initialization routine for the Macintosh */
  6.  
  7. #include    "hack.h"
  8.  
  9. #ifdef MACOS
  10. # ifdef THINK_C
  11. #include    <MemoryMgr.h>
  12. # else
  13. #include    <Memory.h>
  14. #define ApplLimit 0x130         /* application limit [pointer]*/
  15. # endif
  16.  
  17. /* Global variables */
  18. extern WindowPtr    HackWindow;    /* points to NetHack's window */
  19. char    *keys[8];
  20. short macflags;
  21. Boolean    lowMem;
  22. long    lowMemLimit;
  23. typedef struct defaultData {
  24.     long    defaultFlags;
  25.     long    lowMemLimit;
  26.     Str255    fontName;
  27. } defaultData;
  28. #define    fDFZoomWindow    0x02L
  29. #define    fDFUseDefaultFont    0x01L
  30. short altCurs;
  31.  
  32.  
  33. int
  34. initterm(row, col)
  35. short    row, col;
  36. {
  37.     register short    i, j;
  38.     short        tempFont, tempSize, fontNum, size;
  39.     char    *l, *m;
  40.     EventRecord    theEvent;
  41.     FontInfo    fInfo;
  42.     Handle    temp;
  43.     MenuHandle    theMenu;
  44.     OSErr    error;
  45.     Rect        boundsRect;
  46.     Str255    appName, font;
  47.     defaultData    *dD;
  48.     term_info    *t;
  49.     
  50.     /* standard Mac initialization */
  51. #ifdef THINK_C
  52.     SetApplLimit((Ptr)ApplLimit - 8192);    /* an extra 8K for stack */
  53. #else
  54.     SetApplLimit(*(long *)ApplLimit - 8192);
  55. #endif    
  56.     MaxApplZone();
  57.     UnloadSeg(mprintf);
  58.     for (i = 2; i<9; i++) {
  59.         temp = GetResource('CODE', i);
  60.         HUnlock(temp);
  61.         MoveHHi(temp);
  62.         HLock(temp);
  63.     }
  64.     
  65.     MoreMasters();
  66.     MoreMasters();
  67.     MoreMasters();
  68.     MoreMasters();
  69.     lowMem = (FreeMem() < 700 *1024) ? TRUE : FALSE;
  70.     InitGraf(&MAINGRAFPORT);
  71.     
  72.     InitFonts();
  73.     InitWindows();
  74.     InitMenus();
  75.     InitCursor();
  76.     FlushEvents(everyEvent, 0);
  77.     if (error = GetVol((StringPtr)&appName, &tempSize))
  78.         SysBeep(1);
  79.     
  80.     /* Application-specific startup code */
  81.     theMenu = NewMenu(appleMenu, "\001\024");    /*  apple menu  */
  82.     {
  83.         char    tmp[256];
  84.         Sprintf(&tmp[1],"About NetHack %s\311;(-", VERSION);
  85.         tmp[0] = (char)strlen(&tmp[1]);
  86.         AppendMenu(theMenu,tmp);
  87.     }
  88.     AddResMenu(theMenu, 'DRVR');
  89.     InsertMenu(theMenu, 0);
  90.     DisableItem(theMenu,0);
  91.  
  92.     t = (term_info *)malloc(sizeof(term_info));
  93.     t->recordVRefNum = tempSize;
  94.     
  95.     for (i = fileMenu; i <= extendMenu; i++) {
  96.         theMenu = GetMenu(i);
  97.         if (theMenu) {
  98.             InsertMenu(theMenu, 0);
  99.             DisableItem(theMenu, 0);
  100.         }
  101.         if (i == editMenu) {
  102.             t->shortMBarHandle = GetMenuBar();
  103.         }
  104.     }
  105.     t->fullMBarHandle = GetMenuBar();
  106.     
  107.     DrawMenuBar();
  108.     HiliteMenu(0);
  109.     for (i = 0;i <= 7;i++) {
  110.         temp = GetResource(HACK_DATA,(i + 100 + appleMenu));
  111.         if (!temp) {
  112.             SysBeep(1);
  113.             panic("Can't get MENU_DATA resource");
  114.         }
  115.         MoveHHi(temp);
  116.         HLock(temp);
  117.         DetachResource(temp);
  118.         keys[i] = *temp;
  119.     }
  120.  
  121.     macflags = (fToggleNumPad | fDoNonKeyEvt);
  122.     
  123.     /* Set font to monaco, user-defined font or to Hackfont if available */
  124.     if ((SCREEN_BITS.bounds.bottom - SCREEN_BITS.bounds.top) >400
  125.         && (SCREEN_BITS.bounds.right - SCREEN_BITS.bounds.left) > 580)
  126.         size = 12;
  127.     else
  128.         size = 9;
  129.     Strcpy((char *)&font[0], "\006Monaco");
  130.     
  131.     temp = GetResource(HACK_DATA, DEFAULT_DATA);
  132.     if (temp) {
  133.         HLock(temp);
  134.         dD = (defaultData *)(*temp);
  135.         lowMemLimit = dD->lowMemLimit;
  136.         strncpy((char *)&font[0], (char *)&dD->fontName[0],
  137.                     (short)dD->fontName[0] + 1);
  138.         if (dD->defaultFlags & fDFZoomWindow)
  139.             macflags |= fZoomOnContextSwitch;
  140.         HUnlock(temp);
  141.         ReleaseResource(temp);
  142.     }
  143.             
  144.     tempFont = MAINGRAFPORT->txFont;
  145.     tempSize = MAINGRAFPORT->txSize;
  146.     GetFNum(font, &fontNum);
  147.     TextFont(fontNum);
  148.     TextSize(size);
  149.     GetFontInfo(&fInfo);
  150.     TextFont(tempFont);
  151.     TextSize(tempSize);
  152.     
  153.     if (!(dD->defaultFlags & fDFUseDefaultFont)) {
  154.         Strcpy((char *)&appName[0], "\010HackFont");
  155.         GetFNum(appName,&tempFont);
  156.         if (tempFont) {
  157.             fontNum = tempFont;
  158.             tempFont = MAINGRAFPORT->txFont;
  159.             TextFont(fontNum);
  160.             TextSize(size);
  161.             GetFontInfo(&fInfo);
  162.             TextFont(tempFont);
  163.             TextSize(tempSize);
  164.             macflags |= fUseCustomFont;
  165.         }
  166.     }
  167.     
  168.     i = fInfo.ascent + fInfo.descent + fInfo.leading;
  169.     j = fInfo.widMax;
  170.     if ((row * i + 2 * Screen_Border) >
  171.          (SCREEN_BITS.bounds.bottom - SCREEN_BITS.bounds.top)
  172.          ||
  173.          (col * j + 2 * Screen_Border) >
  174.              (SCREEN_BITS.bounds.right - SCREEN_BITS.bounds.left)) {
  175.         size = 9;
  176.         Strcpy((char *)&font[0], "\006Monaco");
  177.         tempFont = MAINGRAFPORT->txFont;
  178.         tempSize = MAINGRAFPORT->txSize;
  179.         GetFNum(font, &fontNum);
  180.         TextFont(fontNum);
  181.         TextSize(size);
  182.         GetFontInfo(&fInfo);
  183.         TextFont(tempFont);
  184.         TextSize(tempSize);
  185.         i = fInfo.ascent + fInfo.descent + fInfo.leading;
  186.         j = fInfo.widMax;
  187.         macflags &= ~fUseCustomFont;
  188.     }        
  189.          
  190.     t->ascent = fInfo.ascent;
  191.     t->descent = fInfo.descent;
  192.     t->height = i;
  193.     t->charWidth = j;
  194.     
  195.     t->fontNum = fontNum;
  196.     t->fontSize = size;
  197.     t->maxRow = row;
  198.     t->maxCol = col;
  199.     t->tcur_x = 0;
  200.     t->tcur_y = 0;
  201.     t->auxFileVRefNum = 0;
  202.     if (error = SysEnvirons(1, &(t->system))) {
  203.         SysBeep(1);
  204.     }
  205.  
  206.     /* Some tweaking to allow for intl. ADB keyboard (unknown) */
  207.     if (t->system.machineType > envMacPlus && !t->system.keyBoardType) {
  208.         t->system.keyBoardType = envStandADBKbd;
  209.     }
  210.  
  211. #define    KEY_MAP    103
  212.     temp = GetResource(HACK_DATA, KEY_MAP);
  213.     if (temp) {
  214.         MoveHHi(temp);
  215.         HLock(temp);
  216.         DetachResource(temp);
  217.         t->keyMap = (char *)(*temp);
  218.     } else
  219.         panic("Can't get keymap resource");
  220.  
  221.     SetRect(&boundsRect, LEFT_OFFSET, TOP_OFFSET + 10,
  222.         (col * fInfo.widMax) + LEFT_OFFSET + 2 * Screen_Border,
  223.         TOP_OFFSET + (row * t->height) + 2 * Screen_Border + 10);
  224.     
  225.     t->screen = (char **)malloc(row * sizeof(char *));
  226.     t->scrAttr = (char **)malloc(row * sizeof(char *));
  227.     l = malloc(row * col * sizeof(char));
  228.     m = malloc(row * col * sizeof(char));
  229.     for (i = 0;i < row;i++) {
  230.         t->screen[i] = (char *)(l + (i * col * sizeof(char)));
  231.         t->scrAttr[i] = (char *)(m + (i * col * sizeof(char)));
  232.     }
  233.     for (i = 0; i < row; i++) {
  234.         for (j = 0; j < col; j++) {
  235.             t->screen[i][j] = ' ';
  236.             t->scrAttr[i][j] = '\0';
  237.         }
  238.     }
  239.     t->curHilite = 0;
  240.     t->curAttr = 0;
  241.  
  242.     /* give time for Multifinder to bring NetHack window to front */
  243.     for(tempFont = 0; tempFont<10; tempFont++) {
  244.         SystemTask();
  245.         (void)GetNextEvent(nullEvent,&theEvent);
  246.     }
  247.  
  248.     HackWindow = NewWindow(0L, &boundsRect, "\016NetHack [MOVE]",
  249.             TRUE, noGrowDocProc, (WindowPtr)-1, FALSE, (long)t);
  250.  
  251.     t->inColor = 0;
  252. #ifdef TEXTCOLOR
  253.     t->color[0] = blackColor;
  254.     t->color[1] = redColor;
  255.     t->color[2] = greenColor;
  256.     t->color[3] = yellowColor;
  257.     t->color[4] = blueColor;
  258.     t->color[5] = magentaColor;
  259.     t->color[6] = cyanColor;
  260.     t->color[7] = whiteColor;
  261.  
  262.     if (t->system.hasColorQD) {
  263.         Rect    r;
  264.         GDHandle    gd;
  265.         
  266.         r = (**(*(WindowPeek)HackWindow).contRgn).rgnBBox;
  267.         LocalToGlobal(&r.top);
  268.         LocalToGlobal(&r.bottom);
  269.         gd = GetMaxDevice(&r);
  270.         t->inColor = (**(**gd).gdPMap).pixelSize > 1;
  271.     }
  272. #endif
  273.         
  274.     temp = GetResource(HACK_DATA, MONST_DATA);
  275.     if (temp) {
  276.         DetachResource(temp);
  277.         MoveHHi(temp);
  278.         HLock(temp);
  279.         i = GetHandleSize(temp);
  280.         mons = (struct permonst *)(*temp);
  281.     } else {
  282.         panic("Can't get MONST resource data.");
  283.     }
  284.     
  285.     temp = GetResource(HACK_DATA, OBJECT_DATA);
  286.     if (temp) {
  287.         DetachResource(temp);
  288.         MoveHHi(temp);
  289.         HLock(temp);
  290.         i = GetHandleSize(temp);
  291.         objects = (struct objclass *)(*temp);
  292.         for (j = 0; j< NROFOBJECTS+1; j++) {
  293.             objects[j].oc_name = sm_obj[j].oc_name;
  294.             objects[j].oc_descr = sm_obj[j].oc_descr;
  295.         }
  296.     } else {
  297.         panic("Can't get OBJECT resource data.");
  298.     }
  299.     
  300.     for (j = 30; j >= 0; j -= 10) {
  301.         for (i = 0; i<=8; i++) {
  302.             t->cursor[i] = GetCursor(100+i+j);    /* self-contained cursors */
  303.         }
  304.     }
  305.     
  306.     (void)aboutBox(0);    
  307.     return 0;
  308. }
  309.  
  310. /* not really even needed. NH never gets to the end of main(), */
  311. /* so this never gets called */
  312. int
  313. freeterm()
  314. {
  315.     return 0;
  316. }
  317.  
  318. #ifdef SMALLDATA
  319. /* SOME [:-( ] Mac compilers have a 32K global & static data limit */
  320. /* these routines help the HANDICAPPED things */
  321. void
  322. init_decl()
  323. {
  324.     short    i;
  325.     char    *l;
  326.     extern char **Map;
  327.     
  328.     l = calloc(COLNO , sizeof(struct rm **));
  329.     level.locations = (struct rm **)l;
  330.     l = calloc(ROWNO * COLNO , sizeof(struct rm));
  331.     for (i = 0; i < COLNO; i++) {
  332.         level.locations[i] = 
  333.         (struct rm *)(l + (i * ROWNO * sizeof(struct rm)));
  334.     }
  335.     
  336.     l = calloc(COLNO , sizeof(struct obj ***));
  337.     level.objects = (struct obj ***)l;
  338.     l = calloc(ROWNO * COLNO , sizeof(struct obj *));
  339.     for (i = 0; i < COLNO; i++) {
  340.         level.objects[i] = 
  341.         (struct obj **)(l + (i * ROWNO * sizeof(struct obj *)));
  342.     }
  343.     
  344.     l = calloc(COLNO , sizeof(struct monst ***));
  345.     level.monsters = (struct monst ***)l;
  346.     l = calloc(ROWNO * COLNO , sizeof(struct monst *));
  347.     for (i = 0; i < COLNO; i++) {
  348.         level.monsters[i] = 
  349.         (struct monst **)(l + (i * ROWNO * sizeof(struct monst *)));
  350.     }
  351.     level.objlist = (struct obj *)0L;
  352.     level.monlist = (struct monst *)0L;
  353.     
  354. l = calloc(COLNO, sizeof(char *));
  355. Map = (char **)l;
  356. l = calloc(ROWNO * COLNO, sizeof(char));
  357. for (i = 0; i < COLNO; i++) {
  358.     Map[i] = 
  359.     (char *)(l + (i * ROWNO * sizeof(char)));
  360. }
  361.  
  362. }
  363.  
  364. /* Since NetHack usually exits before reaching end of main()    */
  365. /* this routine could probably left out.    - J.L.        */
  366. void
  367. free_decl()
  368. {
  369.  
  370.     free((char *)level.locations[0]);
  371.     free((char *)level.locations);
  372.     free((char *)level.objects[0]);
  373.     free((char *)level.objects);
  374.     free((char *)level.monsters[0]);
  375.     free((char *)level.monsters);
  376. }
  377. #endif /* SMALLDATA */
  378.  
  379. #define    OPTIONS            "Nethack prefs"
  380.  
  381. # ifdef AZTEC
  382. #undef OMASK
  383. #define OMASK    O_RDONLY
  384. # else
  385. #undef OMASK
  386. #define OMASK    (O_RDONLY | O_BINARY )
  387. # endif
  388.  
  389. int
  390. read_config_file()
  391. {
  392.     term_info    *t;
  393.     int optfd;
  394.     Str255    name;
  395.     short    oldVol;
  396.     
  397.     optfd = 0;
  398.     t = (term_info *)GetWRefCon(HackWindow);
  399.  
  400.     GetVol(name, &oldVol);
  401.     SetVol(0L, t->system.sysVRefNum);
  402.     if ( (optfd = open(OPTIONS, OMASK)) <= 0) {
  403.         SetVol(0L, t->recordVRefNum);
  404.         optfd = open(OPTIONS, OMASK);
  405.     }
  406.     if ( optfd > (short)NULL){
  407.         read_opts(optfd);
  408.         (void) close(optfd);
  409.     }
  410.     SetVol(0L, oldVol);
  411. }
  412.  
  413. int
  414. write_opts()
  415. {
  416.     int fd;
  417.     short temp_flags;
  418.     term_info    *t;
  419.  
  420.     t = (term_info *)GetWRefCon(HackWindow);
  421.     SetVol(0L, t->system.sysVRefNum);
  422.  
  423.     if((fd = open(OPTIONS, O_WRONLY | O_BINARY)) <= 0) {
  424.         OSErr    result;
  425.         char    *tmp;
  426.         
  427.         SetVol(0L, t->recordVRefNum);
  428.         if((fd = open(OPTIONS, O_WRONLY | O_BINARY)) <= 0) {
  429.             SetVol(0L, t->system.sysVRefNum);
  430.             tmp = CtoPstr(OPTIONS);
  431.             result = Create((StringPtr)tmp, (short)0, CREATOR, AUXIL_TYPE);
  432.              if (result == noErr)
  433.                  fd = open(OPTIONS, O_WRONLY | O_BINARY);
  434.         }
  435.      }
  436.  
  437.     if (fd < 0)
  438.         pline("can't create options file!");
  439.     else {
  440.         write(fd, &flags, sizeof(flags));
  441.     
  442.         write(fd, plname, PL_NSIZ);
  443.     
  444.         write(fd, dogname, 63);
  445.     
  446.         write(fd, catname, 63);
  447.         
  448.         temp_flags = (macflags & fZoomOnContextSwitch) ? 1 : 0;
  449.         write(fd, &temp_flags, sizeof(short));
  450.  
  451.         write(fd, &altCurs, sizeof(short));
  452.     
  453. #ifdef TUTTI_FRUTTI
  454.         write(fd, pl_fruit, PL_FSIZ);
  455. #endif
  456.         write(fd, inv_order, strlen(inv_order)+1);
  457.         close(fd);
  458.     }
  459.     
  460.     SetVol(0L, t->recordVRefNum);
  461.     
  462.     return 0;
  463. }
  464.  
  465. int
  466. read_opts(fd)
  467. int fd;
  468. {    char tmp_order[20];
  469.     short    temp_flags;
  470.     
  471.     read(fd, (char *)&flags, sizeof(flags));
  472.  
  473.     read(fd, plname, PL_NSIZ);
  474.  
  475.     read(fd, dogname, 63);
  476.     
  477.     read(fd, catname, 63);
  478.     
  479.     read(fd, &temp_flags, sizeof(short));
  480.     if (temp_flags & 0x01)
  481.         macflags |= fZoomOnContextSwitch;
  482.     else
  483.         macflags &= ~fZoomOnContextSwitch;
  484.  
  485.     read(fd, &altCurs, sizeof(short));
  486.     
  487. #ifdef TUTTI_FRUTTI
  488.     read(fd, pl_fruit, PL_FSIZ);
  489. #endif
  490.     read(fd,tmp_order,strlen(inv_order)+1);
  491.     if(strlen(tmp_order) == strlen(inv_order))
  492.         Strcpy(inv_order,tmp_order);
  493.         
  494.     return 0;
  495. }
  496.  
  497. #endif /* MACOS */
  498.